﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace wp_XAPandASCX.WebPart1
{
    [ToolboxItemAttribute(false)]
    public class WebPart1 : WebPart
    {
        [WebBrowsable(true),
        Category("Ko Settings"),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName(@"Файл ASCX/XAP в: < _CONTROLTEMPLATES/userControls/*.ascx --- _LAYOUTS/ClientBin/*.xap >: ")]
        public string myUserControl { get; set; }

        Control _myControl;

        protected override void CreateChildControls()
        {

            try
            {
                if (myUserControl != string.Empty && myUserControl.Contains(".xap"))
                {
                    if (myUserControl != string.Empty)
                    {
                        this.Controls.Clear();
                        string renderSLApp = "<div id='mySilverLightHost'>"
                            + "<object data='data:application/x-silverlight-2'"
                            + "type = 'application/x-silverlight-2'>"
                            + "<param name='source' value='_layouts/clientbin/" + myUserControl + "'/>"
                            + "<param name='initParams' value='bg=#ffffff, fg=#0000ff'/>"
                            + "</object></div>";
                        this.Controls.Add(new LiteralControl(renderSLApp));
                    }
                }
                else
                {
                    if (myUserControl != string.Empty)
                    {
                        this.Controls.Clear();
                        _myControl = this.Page.LoadControl(@"/_CONTROLTEMPLATES/userControls/" + myUserControl + "");
                        this.Controls.Add(_myControl);
                    }
                }

            }

            catch (Exception e)
            {
                this.Controls.Add(new LiteralControl(e.Message));
            }
        }

    }
}



